//Shirasu Azusa 2024.5
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<typename T,typename U>
T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);}
template<typename T,typename U>
T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);}
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x)  {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x)  {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x)  {return (x==0?-1:__builtin_ctzll(x));}

#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
  int x=0,w=1; char c=getchar(); 
  while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
  while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();} 
  return x*w;
}

const int N=2005,mod=1e9+7;
int n,a[N],b[N];

namespace Sol {
  int f[N][N][2][2],pp[N],qq[N],r[N],l[N][N],p[N];
  void solve() {
    rep(i,1,n) pp[a[i]]=i;
    rep(i,1,n) qq[b[i]]=i;
    rep(i,1,n) {
      int x=pp[i], y=qq[i];
      p[i]=x-y; l[x][y]=i;
    }
    f[2][1][0][l[1][1]&&p[b[1]]==0]=1;
    f[1][2][1][l[1][1]&&p[a[1]]==0]=1;
    rep(i,1,n) {
      rep(j,1,n) {
        if(i==1&&j==1) continue;
        rep(x,0,1) rep(y,0,1) {
          int cur=(x==0?b[j]:a[i]);
          if(y&&l[i][j]&&i-j==p[cur]&&x==0) {
            f[i][j][x][y]=0;
          }
          //if(f[i][j][x][y]) cout<<i<<" "<<j<<" "<<x<<" "<<y<<" "<<f[i][j][x][y]<<endl;
          rep(z,0,1) {
            int ni=i+(z==0), nj=j+(z==1);
            if(ni>n||nj>n) continue;
            int nw=(z==0?b[j]:a[i]);
            int c1=(y&(p[nw]==p[cur]));
            int c2=(l[i][j]&&p[nw]==i-j);
            int ny=c1|c2;
            (f[ni][nj][z][ny]+=f[i][j][x][y])%=mod;
          }
        }
      }
    }
    int ans=0;
    rep(x,0,1) rep(y,0,1) (ans+=f[n][n][x][y])%=mod;
    printf("%lld\n",ans);
  }
}

signed main() {
  freopen("zha.in","r",stdin);
  freopen("zha.out","w",stdout);
  n=read();
  rep(i,1,n) a[i]=read();
  rep(i,1,n) b[i]=read();
  Sol::solve();
  return 0;
}